using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace HandyIRCChat
{
    public partial class ConsoleForm : Form, IDisposable
    {
        private HandyLogic handyLogic;

        private delegate void SetOutputMessage(string text);

        public ConsoleForm(HandyLogic hl)
        {
            handyLogic = hl;
            InitializeComponent();

            consoleIn.KeyDown += new KeyEventHandler(OnConsoleInKeyDown);
            handyLogic.ConsoleObj.ConsoleMessageEvent +=
                new ConsoleObject.ConsoleMessageEventHandler(
                ConsoleObj_ConsoleMessage);
            handyLogic.ConsoleObj.ReleaseInstanceEvent += 
                new ConsoleObject.ReleaseInstanceEventHandler(
                ConsoleObj_ReleaseInstanceEvent);

            if (hl.ConsoleObj.MessageBuffer.Length != 0)
                WriteOutputMessage(hl.ConsoleObj.MessageBuffer);
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            consoleIn.Focus();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            handyLogic.ConsoleObj.ConsoleMessageEvent -=
                new ConsoleObject.ConsoleMessageEventHandler(
                ConsoleObj_ConsoleMessage);

            handyLogic.Options.ConsoleWindowState.LocX = this.Location.X;
            handyLogic.Options.ConsoleWindowState.LocY = this.Location.Y;
            handyLogic.Options.ConsoleWindowState.SizeX = this.Size.Width;
            handyLogic.Options.ConsoleWindowState.SizeY = this.Size.Height;

            base.OnClosing(e);
        }

        protected override void OnCreateControl()
        {
            this.SetBounds(handyLogic.Options.ConsoleWindowState.LocX,
                handyLogic.Options.ConsoleWindowState.LocY,
                handyLogic.Options.ConsoleWindowState.SizeX,
                handyLogic.Options.ConsoleWindowState.SizeY);

            base.OnCreateControl();
        }

        private void WriteOutputMessage(string text)
        {
            if (this.consoleOut.InvokeRequired)
            {
                SetOutputMessage d = new SetOutputMessage(WriteOutputMessage);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                consoleOut.AppendText(text + "\n");
                consoleOut.ScrollToCaret();
            }
        }

        private void ConsoleObj_ConsoleMessage(object sender, string message)
        {
            WriteOutputMessage(message);
        }

        private void ConsoleObj_ReleaseInstanceEvent(object sender)
        {
            this.Close();
        }

        private void OnConsoleInKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (consoleIn.Text.Length == 0)
                    return;

                StringBuilder sb = new StringBuilder(consoleIn.Text);
                if (sb[0] == '\n')
                    sb.Remove(0, 1);

                handyLogic.ConsoleObj.SendMessage(sb.ToString());
                consoleIn.Clear();
            }
        }
    }
}